package houseSystem.service;

import com.houseSystem.domain.house;

public class houseService {

    private house[] houses;
    private int houseNum = 0;

    public houseService(int size) {
        houses = new house[size];
    }

    public house[] list() {
        return houses;
    }

    public boolean add(house newHouse) {
        if (houseNum >= houses.length) {
            System.out.println("数据存储已满");
            return false;
        }
        houses[houseNum++] = newHouse;
        newHouse.setId(houseNum);
        return true;
    }

    public boolean delete(int id) {
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if (id == houses[i].getId()) {
                index = i;
            }
        }
        if (index == -1) {
            System.out.println("没有该房屋信息");
            return false;
        }
        for (int i = index; i < houseNum - 1; i++) {
            houses[i] = houses[i + 1];
        }
        houses[--houseNum] = null;
        return true;
    }

    public house find(int id) {
        for (int i = 0; i < houseNum; i++) {
            if (houses[i].getId() == id) {
                return houses[i];
            }
        }
        return null;
    }
    
}
